﻿/*
--------------------------------------
File: SimpleColorDetect.cs
Project: Themaopdracht 7
Author: Maarten Kroon

Description:
A very simple color based localisation algorithm
--------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using Costella;

namespace THO7_Team8
{
    class SimpleColorDetect : VisionAlgorithm
    {
        public SimpleColorDetect(String name)
            : base(name)
        {
            hasConfiguration = false;
        }

        public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
        {
            //Image for returning only the license plate without
            Bitmap returnImage = new Bitmap(sourceImage);

            //The original array
            int[] rgbValues = MyImage.BmpToArray(returnImage);
            
            //The modified array
            int size = rgbValues.Length;
            int[] rgbValues2 = new int[size];

            //Yellow color
            int margin = 100;
            int minblue = 17 - (margin / 3); int maxblue = 17 + (margin / 3);
            int mingreen = 204 - margin; int maxgreen = 204 + margin;
            int minred = 255 - margin; int maxred = 255 + margin;

            //Ints for the first and last x and y yellow pixels
            int firstX = returnImage.Width, firstY = 0, lastX = 0, lastY = 0;
            int firstXd = 0;

            //Main loop which checks for yellow pixels
            for (int i = 0; i < size; i++)
            {
                int blue = rgbValues[i] & 255;
                int green = (rgbValues[i] >> 8) & 255;
                int red = (rgbValues[i] >> 16) & 255;

                //If the current pixel is yellow
                if (blue <= maxblue &&
                    blue >= minblue &&
                    green <= maxgreen &&
                    green >= mingreen &&
                    red <= maxred &&
                    red >= minred)
                {
                    //The first Y pixel is the first yellow pixel
                    if (firstY == 0)
                    {
                        firstY = i;
                    }
                    //The first X pixel is the most left yellow pixel
                    if (firstX > (i % returnImage.Width))
                    {
                        firstX = i % returnImage.Width;
                        firstXd = i;
                    }
                    //The last X pixel is the most right yellow pixel
                    if ((i % returnImage.Width)  > (lastX % returnImage.Width))
                    {
                        lastX = i % returnImage.Width;
                    }
                    //The last Y pixel is the last yellow pixel
                    lastY = i;

                    //If the current pixel is yellow, copy the color from the original image into the modified array
                    rgbValues2[i] = rgbValues[i];      
                }
                else
                {
                    //If the current pixel is not yellow, make it black in the modified image array
                    rgbValues2[i] = -16777216;
                }
            }
            //Return image contains only the license plate
            returnImage = MyImage.ArrayToBmp(rgbValues2, returnImage);
            //HighlightedImage contains all the original pixels
            Bitmap highlightedImage = (Bitmap)sourceImage.Clone();
            //Adds a colored rectangle surrounding the license plate
            Highlight.drawRectangleOnBitmap(highlightedImage, firstX, firstY / returnImage.Width, lastX - firstX, (lastY / returnImage.Width) - (firstY / returnImage.Width), Color.DeepPink);
            return highlightedImage;
        }
    }
}







